package com.lq.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lq.user.entity.User;
import com.lq.user.entity.UserInfo;
import com.lq.user.mapper.UserMapper;
import com.lq.user.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lq
 * @since 2025-08-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public User getByUsername(String username) {
        return getOne(new QueryWrapper<User>().eq("username", username));
    }

    @Override
    public User getByPhone(String phone) {
        return getOne(new QueryWrapper<User>().eq("phone", phone));
    }

    @Override
    public boolean register(User user) {
        // 检查用户名是否已存在
        if (getByUsername(user.getUsername()) != null) {
            throw new RuntimeException("用户名已存在");
        }

        // 如果提供了手机号，检查手机号是否已存在
        if (user.getPhone() != null && !user.getPhone().isEmpty()) {
            if (getByPhone(user.getPhone()) != null) {
                throw new RuntimeException("手机号已存在");
            }
        }

        // 密码加密
        String encryptedPassword = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(encryptedPassword);

        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setStatus(1); // 默认启用状态

        return save(user);
    }

    @Override
    public boolean login(String username, String password) {
        User user = getByUsername(username);
        if (user == null) {
            return false;
        }

        // 密码加密后比较
        String encryptedPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        return user.getPassword().equals(encryptedPassword) && user.getStatus() == 1;
    }

    @Override
    public boolean updateStatus(Long userId, Integer status) {
        User user = getById(userId);
        if (user != null) {
            user.setStatus(status);
            user.setUpdateTime(LocalDateTime.now());
            return updateById(user);
        }
        return false;
    }

    @Override
    public boolean changePassword(Long userId, String oldPassword, String newPassword) {
        User user = getById(userId);
        if (user != null) {
            // 验证旧密码
            String encryptedOldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
            if (user.getPassword().equals(encryptedOldPassword)) {
                // 更新为新密码
                String encryptedNewPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());
                user.setPassword(encryptedNewPassword);
                user.setUpdateTime(LocalDateTime.now());
                return updateById(user);
            }
        }
        return false;
    }

}